package test5.lupeng.jf.com.teamdemo.untils;

import android.content.Context;

import test5.lupeng.jf.com.teamdemo.bean.response.LoginResponse;


/**
 *  单例模式
 *  记录数据的工具类
 * Created by jf on 2017/7/26.
 */

public class AppConfigUtils {

    private AppConfigUtils(){}
    private static AppConfigUtils mConfigUtils;

    /**
     *  单例模式下只能通过该方法获取到该类的对象, 且是唯一的
     * @return
     */
    public static final AppConfigUtils getInstance(){
        if (mConfigUtils == null){
            mConfigUtils = new AppConfigUtils();
        }
        return mConfigUtils;
    }

    private String KEY_ISLOGIN = "isLogin";
    /**
     *  设置当前的登录状态
     * @param context  上下文
     * @param value  要存储的boolean 类型的值
     */
    public void setLoginStatus(Context context, boolean value){
        SpUtils.putBoolean(context, KEY_ISLOGIN, value);
    }

    /**
     *  获取当前的登录状态
     * @param context 上下文
     * @return
     */
    public boolean getLoginStatus(Context context){
        return SpUtils.getBoolean(context, KEY_ISLOGIN);
    }

    static class UserInfo {
        public static final String sessionToken = "sessionToken";
        public static final String updatedAt = "updatedAt";
        public static final String objectId = "objectId";
        public static final String username = "username";
        public static final String createdAt = "createdAt";
        public static final String headProtrait = "headProtrait";
        public static final String signature = "signature";
        public static final String emailVerified = "emailVerified";
        public static final String mobilePhoneVerified = "mobilePhoneVerified";

    }

    /**
     * 保存用户信息
     * @param context
     * @param loginResponse
     */
    public void saveLoginInfo(Context context, LoginResponse loginResponse) {
        SpUtils.putString(context, UserInfo.sessionToken, loginResponse.getSessionToken());
        SpUtils.putString(context, UserInfo.updatedAt, loginResponse.getUpdatedAt());
        SpUtils.putString(context, UserInfo.objectId, loginResponse.getObjectId());
        SpUtils.putString(context, UserInfo.username, loginResponse.getUsername());
        SpUtils.putString(context, UserInfo.createdAt, loginResponse.getCreatedAt());
        SpUtils.putString(context, UserInfo.headProtrait, loginResponse.getHeadProtrait());
        SpUtils.putString(context, UserInfo.signature,loginResponse.getSignature());

    }

    static LoginResponse loginResponse;

    /**
     * 获取用户信息
     * @param context
     * @return
     */
    public LoginResponse getLoginInfo(Context context) {
        if (loginResponse == null) {
            loginResponse = new LoginResponse();
            loginResponse.setSessionToken(SpUtils.getString(context, UserInfo.sessionToken));
            loginResponse.setUpdatedAt(SpUtils.getString(context, UserInfo.updatedAt));
            loginResponse.setObjectId(SpUtils.getString(context, UserInfo.objectId));
            loginResponse.setUsername(SpUtils.getString(context, UserInfo.username));
            loginResponse.setCreatedAt(SpUtils.getString(context, UserInfo.createdAt));
            loginResponse.setHeadProtrait(SpUtils.getString(context, UserInfo.headProtrait));
            loginResponse.setSignature(SpUtils.getString(context, UserInfo.signature));
        }
        return loginResponse;
    }


    /**
     * 清除所有数据
     * @param context
     * @return
     */
    public boolean clearLoginInfo(Context context) {
        mConfigUtils = null;
        return SpUtils.clearData(context);
    }





}
